<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="产品ID" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入产品ID" />
                </el-form-item>
                <el-form-item label="分类ID" prop="categoryId">
                    <el-input v-model="formData.categoryid" placeholder="请输入分类ID" />
                </el-form-item>
                <el-form-item label="产品名称" prop="name">
                    <el-input v-model="formData.name" placeholder="请输入产品名称" />
                </el-form-item>
                <el-form-item label="产品描述" prop="description">
                    <el-input
                        v-model="formData.description"
                        placeholder="请输入产品描述"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="主图" prop="image">
                    <material-picker v-model="formData.image" />
                </el-form-item>
<!--                <el-form-item label="产品图片集，JSON格式" prop="images">-->
<!--                    <el-input-->
<!--                        v-model="formData.images"-->
<!--                        placeholder="请输入产品图片集，JSON格式"-->
<!--                        type="textarea"-->
<!--                        :autosize="{ minRows: 4, maxRows: 6 }"-->
<!--                    />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="产品详情" prop="details">-->
<!--                    <el-input-->
<!--                        v-model="formData.details"-->
<!--                        placeholder="请输入产品详情"-->
<!--                        type="textarea"-->
<!--                        :autosize="{ minRows: 4, maxRows: 6 }"-->
<!--                    />-->
<!--                </el-form-item>-->
                <el-form-item label="产品详情" prop="details">
                  <editor v-model="formData.details" :height="667" :width="375" />
                </el-form-item>
<!--                <el-form-item label="搜索关键词" prop="keywords">-->
<!--                    <el-input v-model="formData.keywords" placeholder="请输入搜索关键词" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="是否热门：0-否，1-是" prop="isHot">-->
<!--                    <el-radio-group-->
<!--                        v-model="formData.ishot"-->
<!--                        placeholder="请输入是否热门：0-否，1-是"-->
<!--                    >-->
<!--                        <el-radio label="0">否</el-radio>-->
<!--                        <el-radio label="1">是</el-radio>-->
<!--                    </el-radio-group>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="是否最新" prop="isNew">-->
<!--                    <el-radio-group-->
<!--                        v-model="formData.isnew"-->
<!--                        placeholder="请输入是否最新：0-否，1-是"-->
<!--                    >-->
<!--                        <el-radio label="0">否</el-radio>-->
<!--                        <el-radio label="1">是</el-radio>-->
<!--                    </el-radio-group>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="排序" prop="sort">-->
<!--                    <el-input v-model="formData.sort" placeholder="请输入排序" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="状态：0-下架，1-上架" prop="status">-->
<!--                    <el-radio-group-->
<!--                        v-model="formData.status"-->
<!--                        placeholder="请选择状态：0-下架，1-上架"-->
<!--                    >-->
<!--                        <el-radio label="0">下架</el-radio>-->
<!--                        <el-radio label="1">上架</el-radio>-->
<!--                    </el-radio-group>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="浏览量" prop="views">-->
<!--                    <el-input v-model="formData.views" placeholder="请输入浏览量" />-->
<!--                </el-form-item>-->
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import type { PropType } from 'vue'

import { productAdd, productDetail, productEdit } from '@/api/product'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑产品' : '新增产品'
})

const formData = reactive({
    id: '',
    categoryId: '',
    name: '',
    description: '',
    image: '',
    images: '',
    details: '',
    keywords: '',
    isHot: '',
    isNew: '',
    sort: '',
    status: '',
    views: ''
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入产品ID',
            trigger: ['blur']
        }
    ],
    categoryid: [
        {
            required: true,
            message: '请输入分类ID',
            trigger: ['blur']
        }
    ],
    name: [
        {
            required: true,
            message: '请输入产品名称',
            trigger: ['blur']
        }
    ],
    description: [
        {
            required: true,
            message: '请输入产品描述',
            trigger: ['blur']
        }
    ],
    image: [
        {
            required: false,
            message: '请选择主图',
            trigger: ['blur']
        }
    ],
    images: [
        {
            required: true,
            message: '请输入产品图片集，JSON格式',
            trigger: ['blur']
        }
    ],
    details: [
        {
            required: true,
            message: '请输入产品详情',
            trigger: ['blur']
        }
    ],
    keywords: [
        {
            required: true,
            message: '请输入搜索关键词',
            trigger: ['blur']
        }
    ],
    ishot: [
        {
            required: true,
            message: '请输入是否热门：0-否，1-是',
            trigger: ['blur']
        }
    ],
    isnew: [
        {
            required: true,
            message: '请输入是否最新：0-否，1-是',
            trigger: ['blur']
        }
    ],
    sort: [
        {
            required: true,
            message: '请输入排序',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择状态：0-下架，1-上架',
            trigger: ['blur']
        }
    ],
    views: [
        {
            required: true,
            message: '请输入浏览量',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await productEdit(data) : await productAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await productDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
